//
//  ShowsViewController.m
//  Crocodile
//
//  Created by CONG TRAN on 9/6/12.
//  Copyright (c) 2012 hust. All rights reserved.
//

#import "ShowsViewController.h"
#import "MapViewController.h"
#import "Annotation.h"
#import "Constant.h"
#import "ShowViewDetailViewController.h"

@interface ShowsViewController ()

@end

@implementation ShowsViewController
@synthesize showsTable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createData];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    UIButton *buttonTest = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 40, 40)];
//    buttonTest.titleLabel.text = @"show in maps";
//    buttonTest.backgroundColor = [UIColor blueColor];
//    [buttonTest addTarget:self action:@selector(viewmap) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:buttonTest];
//	// Do any additional setup after loading the view.
    
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kImgBackground]];
    self.parentViewController.view.alpha = 1.00;
    
    // Header Image
    UIImage *headerImg = [UIImage imageNamed:kImgHeader];
    UIImageView *headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [headerImgView setImage:headerImg];
    [self.view addSubview:headerImgView];
    [headerImg release];
    [headerImgView release];
    
    // Title
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 170, 34)];
    titleLbl.backgroundColor = [UIColor clearColor];
    titleLbl.center = CGPointMake(160, 20);
    titleLbl.textAlignment = UITextAlignmentCenter;
    titleLbl.textColor = [UIColor greenColor];
    titleLbl.font = [UIFont boldSystemFontOfSize:20];
    titleLbl.text = kStrShowTabTitle;
    [self.view addSubview:titleLbl];
    [titleLbl release];
    
    [self loadTable];
}

-(void) viewmap
{
    MapViewController *mapView = [[MapViewController alloc]init];
    [self.navigationController pushViewController:mapView animated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - (Create Data and Table)
-(void) createData
{
    annonationArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 10 ; i ++) 
    {
        CLLocationCoordinate2D location;
        location.latitude = 21.033333 + i;
        location.longitude = 105.850000 + i;
        Annotation *annonation = [[Annotation alloc]initWithName:location title:@"Nhà hát lớn" subTitle:@"Nhà hát lớn Hà Nội" date:@"2012-12-30" andContent:@"Buổi biểu diễn tại nhà hát lớn Hà nội diễn ra ngày 30/11/2012"];
        [annonationArray addObject:annonation];
    }
    
}

-(void) loadTable
{
    showsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 320, 480) style:UITableViewStylePlain];
    showsTable.backgroundColor = [UIColor clearColor];
    showsTable.separatorColor = [UIColor clearColor];
    showsTable.delegate = self;
    showsTable.dataSource = self;
    [self.view addSubview:showsTable];
}

#pragma mark - TableView delege
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [annonationArray count];
    NSLog(@"%d",[annonationArray count]);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = nil;
    UIImageView *showsDateImage, *continueImageView;
    UILabel *titleLabel, *subTitleLabel, *timeLabel;
    Annotation *annonation = [annonationArray objectAtIndex:indexPath.row];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kImgCellBg]];
        bgView.alpha = 0.1;
        cell.backgroundView = bgView;
        
        
        showsDateImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 40, 40)];
        [showsDateImage setImage:[UIImage imageNamed:@"EventDateBackground.png"]];
        showsDateImage.alpha = 0.3;
        [cell addSubview:showsDateImage];
        [showsDateImage release];
        
        continueImageView = [[UIImageView alloc]initWithFrame:CGRectMake(260, 30, 40, 40)];
        [continueImageView setImage:[UIImage imageNamed:@"l_arrow_right.png"]];
        continueImageView.alpha = 0.6;
        [cell addSubview:continueImageView];
        [continueImageView release];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 5, 300, 30)];
        [titleLabel setTag:1000];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        //titleLabel.text = annonation.titleAnnonation;
        [cell addSubview:titleLabel];
        [titleLabel release];
        
        subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 25, 300, 30)];
        [subTitleLabel setTag:1001];
        subTitleLabel.backgroundColor = [UIColor clearColor];
        subTitleLabel.textColor = [UIColor grayColor];
        subTitleLabel.font = [UIFont systemFontOfSize:12];
        [cell addSubview:subTitleLabel];
        [subTitleLabel release];
        
        timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 45, 300, 30)];
        [timeLabel setTag:1002];
        timeLabel.textColor = [UIColor grayColor];
        timeLabel.backgroundColor = [UIColor clearColor];
        timeLabel.font = [UIFont systemFontOfSize:12];
        [cell addSubview:timeLabel];
        [timeLabel release];
        
    }
    
    titleLabel = (UILabel*)[cell viewWithTag:1000];
    titleLabel.text = annonation.titleAnnonation;
    
    subTitleLabel = (UILabel*)[cell viewWithTag:1001];
    subTitleLabel.text = annonation.subTitle;
    
    timeLabel = (UILabel*)[cell viewWithTag:1002];
    timeLabel.text = annonation.date;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShowViewDetailViewController *showDetail = [[ShowViewDetailViewController alloc]init];
    showDetail.annonation = [annonationArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:showDetail animated:YES];
    [showDetail release];
}

@end
